#coding:utf-8
'''
Created on 2009-12-6

@author: Administrator
'''


from google.appengine.ext import webapp
from google.appengine.api import users
from wordpresslib import WordPressClient,WordPressPost
from google.appengine.api import mail
import Auths,logging

blogs = {"jamiesuncn":"https://jamiesuncn.wordpress.com/xmlrpc.php",
         "pythoneye":"https://pythoneye.wordpress.com/xmlrpc.php",
         "sharereader":"https://sharereader.wordpress.com/xmlrpc.php",
         "goddessss":"https://goddessss.wordpress.com/xmlrpc.php"
         }
blog_emails = {"jamiesuncn":"ruco63hevi@post.wordpress.com",
         "pythoneye":"yuyo01dini@post.wordpress.com",
         "sharereader":"qiwi46bipi@post.wordpress.com",
         "goddessss":"yeja74tazi@post.wordpress.com"
         }
class WordpressCategoryHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        cats = []
        blog = self.request.get("blog")
        if blog:
            wp = WordPressClient(blogs[blog], Auths.wordpressUser(), Auths.wordpressPasswd())
            try:
                cats = wp.getCategoryList()
            except:
                logging.error("wp.getCategoryList() error")
        if cats:
            linkstr = """<a onclick="selectCategorys('%s')" href="void:return 0;">%s</a>"""
            catlinks = "   ".join([linkstr%(it.name,it.name) for it in cats])
            self.response.out.write(catlinks)   
                 

class WordpressHandler(webapp.RequestHandler):
    def get(self):
        #is login
        self.response.headers['Content-Type'] = 'text/html'
        user = users.get_current_user()
        if not user:
            login = ("<a href=\"%s\">Sign in </a>." %users.create_login_url("/zuosa"))
            self.response.out.write("<html><body>%s</body></html>" % login)
            
        #blog = self.request.get("blog")
        title = self.request.get("title")
        source = self.request.get("source")
        url = self.request.get("url")     
        linkstr = """<a onclick="getCategorys('%s')" href="void:return 0;">%s</a>"""
        bloglinks = " ".join([linkstr%(k,k) for k in blogs.keys()])

        html = """
                <script src="js/wordpress.js"></script>
                <link href="css/wordpress.css" rel="stylesheet" type="text/css" />
                <form action="/wordpress" method="post" enctype="application/x-www-form-urlencoded" target="_self">
                  <div align="center">
                    <div id="main" >Blog:%s
                      <input name="blog" type="text" id="blog"/>
                      Category:<div id="categorys"></div>
                      <input name="cat" type="text" id="cat" />
                      Tags(with ,):
                      <input name="tags" type="text" id="tags" />                      
                      Title:
                      <input name="title" type="text" id="title" value="%s"/>
                      Content:                                 
                      <textarea name="desc" rows="20" id="desc"><strong>Read More:</strong> <a href="%s">%s</a>
                      </textarea>
                      <input name="Submit" type="submit" class="submit" value="Submit" />
                    </div>
                  </div>
                </form>""" % (bloglinks,title,url,source+"--"+title)
        self.response.out.write(html)
    
    
    def post(self):
        self.response.headers['Content-Type'] = 'text/html'
        user = users.get_current_user()
        if not user:
            self.response.out.write("Illegal Operation")
        else:
            blog = self.request.get("blog")
            cat = self.request.get("cat")
            title = self.request.get("title")
            desc = self.request.get("desc")
            tags = self.request.get("tags")
            if not tags:
                tags = cat
            
        if not blog or not cat or not title or not desc:
            self.response.out.write(u"blog=%s cat=%s title=%s desc=%s"%(blog,
                                                                       cat,
                                                                       title,
                                                                       desc))
            self.response.out.write(" <p>please input full data</p>")
        else:
            try:
#                title_lab = u"[title %s]"%title
#                #tags_lab = u"[tags %s]"%tags
#                cats_lab = u"[category %s]"%cat
#                #stats_lab = u"[status Published]"
#                mail.send_mail(sender=user.email(),
#                    to=blog_emails[blog], 
#                    subject=title,
#                    body=desc+title_lab+cats_lab)
                    wp = WordPressClient(blogs[blog], Auths.wordpressUser(), Auths.wordpressPasswd())
                    post = WordPressPost()
                    post.title = title
                    post.description = desc
                    post.categories = (wp.getCategoryIdFromName(cat),)
                    wp.newPost(post, True)           
            except:
                import sys
                info = sys.exc_info()
                logging.error("error post blog to %s %s %s"%(blog,info[0],info[1]))   
                
            self.response.out.write("<script>window.close();</script>")                   
 
            
            
            
    
    
    
    
    